const sqlite3 = require('sqlite3').verbose()

const Genid = require("../utils/SnowFlake.js")

const path = require('path')

var db = new sqlite3.Database(path.join(__dirname,'blog.sqlite3'))

const genid = new Genid({
    WorkerId:1,
})
//promise封装解决回调地狱
db.async = {} 

//查询
db.async.all = (sql,params) =>{
    return new Promise((resolve,reject)=>{
        db.all(sql,params,(err,rows)=>{
            resolve({err,rows})
        })
    })
}
//增删改
db.async.run = (sql,params) =>{
    return new Promise((resolve,reject)=>{
        db.all(sql,params,(err,rows)=>{
            resolve({err,rows})
        })
    })
}

module.exports = {db,genid}